#!/usr/bin/env bash
cd `dirname $0`
source ../../config.sh
exec_dir semester_report

HIVE_DB=model
HIVE_TABLE=semester_report
TARGET_TABLE=semester_report

function create_table(){

    hadoop fs -rm -r ${BASE_HIVE_DIR}/${HIVE_DB}/${HIVE_TABLE} || :

	hive -e "DROP TABLE IF EXISTS ${HIVE_DB}.${HIVE_TABLE};"

	hive -e "CREATE EXTERNAL TABLE IF NOT EXISTS ${HIVE_DB}.${HIVE_TABLE}(
            semester_year STRING COMMENT '学年',
			semester String comment '学期',
			cooperate_num   INT comment'校企合作联合开发课程数',
			share_num    INT comment'精品资源共享课数'

      )COMMENT '学期划分填报信息表'
    ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
    LOCATION '${BASE_HIVE_DIR}/${HIVE_DB}/${HIVE_TABLE}'
   "
    fn_log "创建表--学期划分填报信息表 :${HIVE_DB}.${HIVE_TABLE}"
}

function import_table(){

    ###################################################
    ###加载本地数据
    ###################################################

    hive -e "LOAD DATA LOCAL INPATH '../../csv/semester_report.csv' INTO TABLE ${HIVE_DB}.${HIVE_TABLE};"

    fn_log "加载本地数据到"

    hive -e "
           INSERT OVERWRITE TABLE model.semester_report
        SELECT
        a.semester_year,
        a.semester,
		a.cooperate_num,
		a.share_num		
		from
        (
            select
            row_number() over(order by a.semester_year desc) as num,
            a.*
            from ${HIVE_DB}.${HIVE_TABLE} a
           ) a
           where a.num>1 "

    fn_log "导入数据--学期划分填报信息表 :${HIVE_DB}.${HIVE_TABLE}"
}
create_table
import_table

